import { Space, Alert, Modal, message } from 'antd'
import {
  queryModelParentRelation,
  deleteOne,
  cloneModel
} from '../apis/model'
import { useState } from 'react'
import { createColumn } from '../components/common/CommonTable'
import UserName from '../components/UserName'

const useModeler = ({
  modelType,
  deleteNeedChooseCascade = false,
  doQueryList
}) => {
  const modelerListColumns = [
    {
      key: 'name',
      dataIndex: 'name',
      title: '名称'
    },
    createColumn.version({
      dataIndex: 'version',
      title: '当前版本'
    }),
    {
      key: 'createdBy',
      dataIndex: 'createdBy',
      title: '创建',
      width: 120,
      align: 'center',
      render: (userId) => {
        return <UserName userId={userId}></UserName>
      }
    },
    {
      key: 'lastUpdatedBy',
      dataIndex: 'lastUpdatedBy',
      title: '上次更新',
      width: 120,
      align: 'center',
      render: (userId) => {
        return <UserName userId={userId}></UserName>
      }
    },
    createColumn.time({
      dataIndex: 'lastUpdated',
      title: '更新时间',
      width: 180
    }),
  ]

  const sortOptions = [
    {
      value: 'modifiedDesc',
      label: '按照时间修改降序'
    },
    {
      value: 'modifiedAsc',
      label: '按照时间修改升序'
    },
    {
      value: 'nameDesc',
      label: '按照名称降序'
    },
    {
      value: 'nameAsc',
      label: '按照名称升序'
    }
  ]

  const formItems = [
    {
      name: 'name',
      label: '模型名称',
      required: true
    },
    {
      name: 'key',
      label: '模型key',
      required: true
    },
    {
      name: 'description',
      label: '描述',
      type: 'textarea'
    }
  ]

  const commonSetting = {
    modelerListColumns,
    sortOptions,
    formItems
  }

  const [cloneIngModel, setCloneIngModel] = useState({})

  const handleClone = (rowData) => {
    setCloneIngModel(rowData)
  }

  const handleCancelClone = () => {
    setCloneIngModel({})
  }

  const handleSubmitClone = async (values) => {
    try {
      await cloneModel({
        id: cloneIngModel.id,
        modelType,
        ...values
      })
      handleCancelClone()
      doQueryList()
      message.success('复制成功')
    } catch (error) {
      message.error(error.message || '复制失败')
    }
  }

  // 删除操作
  const handleDeleteModel = async (rowData) => {
    const relations = await queryModelParentRelation(rowData.id)
    if (relations && relations.length) {
      Modal.warning({
        width: 500,
        title: '无法删除此模型，因为另一个模型正在使用它',
        content: (<Space
          direction="vertical"
          style={{ width: '100%' }}
        >
          {
            relations.map(el => <Alert
              key={el.id}
              message={el.name}
              type="warning"
            />)
          }
        </Space>)
      })
    } else {
      if (deleteNeedChooseCascade) {

      } else {
        Modal.confirm({
          title: '确定要删除？',
          onOk: async () => {
            try {
              await deleteOne(rowData.id)
              message.success('删除成功')
              doQueryList()
            } catch (error) {
              message.error(error.message || '删除成功')
            }
          }
        })
      }
    }
  }

  return {
    commonSetting,
    cloneIngModel,
    handleClone,
    handleCancelClone,
    handleSubmitClone,
    handleDeleteModel
  }
}

export default useModeler